<template>
  <div class="app-container">
    <!-- 添加和修改章节表单 -->
    <el-dialog :visible="dialogVisible" title="添加章节" @close="close()">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import chapterApi from '@/api/vid/chapter'
export default {
  name: "form",
  data() {
    return {
      dialogVisible: false,
      chapter: {
        sort: 0
      }
    }
  },
  created() {

  },
  methods: {
    open(chapterId) {
      this.dialogVisible = true
      if (chapterId){
        chapterApi.getChapterById(chapterId).then(res => {
          this.chapter = res.data
        })
      }
    },
    close(){
      this.dialogVisible = false
      this.chapter = {
        sort: 0
      }
    },
    saveOrUpdate() {
      if (this.chapter.id){
        chapterApi.updateChapter(this.chapter).then(res => {
          this.$message.success('章节修改成功！')
          this.close()
          this.$parent.getNodeList()
        })
      }else {
        this.chapter.courseId = this.$parent.$parent.courseId
        chapterApi.saveChapter(this.chapter).then(res => {
           this.$message.success('章节添加成功！')
           this.close()
           this.$parent.getNodeList()
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
